﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wicresoft.Framework.Organization;
using Wicresoft.JDPostBoard.Entity;
using System.Data;
using System.Text;
using Wicresoft.Common;

public partial class ejiading_Detail : EJDBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {


            ApplyLog apply = new ApplyLog(ApplyID);
            if (apply.Load())
                LoadFromEntity(apply);

        }
    }

    protected void LoadFromEntity(ApplyLog log)
    {
        this.ltApplicant.Text = log.ApplierDsName;
        this.ltDepart.Text = log.ApplierDepartment;
        this.ltTime.Text = log.ApplyTime.ToString();
        this.ltTitle.Text = log.ApplyTitle;
        this.ltRemark.Text = log.Remark;
        this.tbShyj.Value = log.DealContent;
        this.ltTotal.Text = log.ApplyCost.ToString("N");


        if (log.AuditorID != "")
        {
            ListItem itemAll = new ListItem();
            itemAll.Text = log.AuditorDisplay;
            itemAll.Value = log.AuditorID;
            this.ddlAuditor.Items.Insert(0, itemAll);

        }

        ListItem li = ddlAuditor.Items.FindByValue(log.AuditorID);
        if (li != null)
        {
            this.ddlAuditor.SelectedValue = li.Value;
        }
        if (log.ArrivalTime == DateTime.MinValue)
        {
            this.ltArrivalTime.Text = string.Empty;
        }
        else
        {
            this.ltArrivalTime.Text = log.ArrivalTime.ToString();
        }

        if (log.Status == Enum.ApplyStatusEnum.待审核.ToString() ||
            log.Status == Enum.ApplyStatusEnum.延后审核.ToString() ||
             log.Status == Enum.ApplyStatusEnum.延后审核已发货.ToString())
        {
            btnPass.Visible = true;
            btnNotPass.Visible = true;
        }
        DataTable dt = DataAccess.Select(typeof(Apply_Gift_Relation), "ApplyId='"+log.ApplyID+"'");
        string wupin = string.Empty;
        foreach (DataRow item in dt.Rows)
        {
            wupin += item["GiftName"].ToString() + item["GiftCount"].ToString() + item["GiftUnit"].ToString() + "份,";
        }
        wupin = wupin.Trim(new char[] { ',' });
        ltTotal.Text = wupin;

    }
    public Guid ApplyID
    {
        get
        {
            if (Request["ApplyID"] != null)
                return new Guid(Request["ApplyID"]);
            return Guid.Empty;
        }
    }


    protected void PassSupply(object sender, EventArgs e)
    {
        ApplyLog ol = new ApplyLog(ApplyID);
        ol.Load();
        if (ol.Status == Enum.ApplyStatusEnum.待审核.ToString())
        {
            ol.Status = Enum.ApplyStatusEnum.审核通过.ToString();
            ol.DealTime = DateTime.Now;

            ol.DealerID = this.ddlAuditor.SelectedValue;
            //ol.DealerName = CurrentUser.Name;
            ol.DealerDsName = this.ddlAuditor.SelectedItem.Text;

            ol.RealDealerID = CurrentUser.ID.ToString();
            ol.RealDealerName = CurrentUser.Name;
            ol.RealDealerDisplay = CurrentUser.DisplayName;

            ol.DealContent = this.tbShyj.Value;
            ol.Save();

        }
        if (ol.Status == Enum.ApplyStatusEnum.延后审核.ToString())
        {
            ol.Status = Enum.ApplyStatusEnum.审核通过.ToString();
            ol.DealTime = DateTime.Now;

            ol.DealerID = this.ddlAuditor.SelectedValue;
            //ol.DealerName = CurrentUser.Name;
            ol.DealerDsName = this.ddlAuditor.SelectedItem.Text;

            ol.RealDealerID = CurrentUser.ID.ToString();
            ol.RealDealerName = CurrentUser.Name;
            ol.RealDealerDisplay = CurrentUser.DisplayName;

            ol.DealContent = this.tbShyj.Value;
            ol.Save();

        }
        if (ol.Status == Enum.ApplyStatusEnum.延后审核已发货.ToString())
        {
            ol.Status = Enum.ApplyStatusEnum.已领取.ToString();
            ol.DealTime = DateTime.Now;

            ol.DealerID = this.ddlAuditor.SelectedValue;
            //ol.DealerName = CurrentUser.Name;
            ol.DealerDsName = this.ddlAuditor.SelectedItem.Text;

            ol.RealDealerID = CurrentUser.ID.ToString();
            ol.RealDealerName = CurrentUser.Name;
            ol.RealDealerDisplay = CurrentUser.DisplayName;

            ol.DealContent = this.tbShyj.Value;
            ol.Save();

        }

        ClientScript.RegisterStartupScript(this.GetType(), "RefreshParent", "<script>location=location;</script>");

    }
    protected void NotPassSupply(object sender, EventArgs e)
    {
        ApplyLog ol = new ApplyLog(ApplyID);
        ol.Load();
        if (ol.Status == Enum.ApplyStatusEnum.待审核.ToString())
        {
            ol.Status = Enum.ApplyStatusEnum.审核未通过.ToString();
            ol.DealTime = DateTime.Now;

            ol.DealerID = this.ddlAuditor.SelectedValue;
            //ol.DealerName = CurrentUser.Name;
            ol.DealerDsName = this.ddlAuditor.SelectedItem.Text;

            ol.RealDealerID = CurrentUser.ID.ToString();
            ol.RealDealerName = CurrentUser.Name;
            ol.RealDealerDisplay = CurrentUser.DisplayName;

            ol.DealContent = this.tbShyj.Value;
            ol.Save();
        }
        if (ol.Status == Enum.ApplyStatusEnum.延后审核.ToString())
        {
            ol.Status = Enum.ApplyStatusEnum.审核未通过.ToString();
            ol.DealTime = DateTime.Now;

            ol.DealerID = this.ddlAuditor.SelectedValue;
            //ol.DealerName = CurrentUser.Name;
            ol.DealerDsName = this.ddlAuditor.SelectedItem.Text;

            ol.RealDealerID = CurrentUser.ID.ToString();
            ol.RealDealerName = CurrentUser.Name;
            ol.RealDealerDisplay = CurrentUser.DisplayName;

            ol.DealContent = this.tbShyj.Value;
            ol.Save();
        }
        if (ol.Status == Enum.ApplyStatusEnum.延后审核已发货.ToString())
        {
            ol.Status = Enum.ApplyStatusEnum.审核未通过.ToString();
            ol.DealTime = DateTime.Now;

            ol.DealerID = this.ddlAuditor.SelectedValue;
            //ol.DealerName = CurrentUser.Name;
            ol.DealerDsName = this.ddlAuditor.SelectedItem.Text;

            ol.RealDealerID = CurrentUser.ID.ToString();
            ol.RealDealerName = CurrentUser.Name;
            ol.RealDealerDisplay = CurrentUser.DisplayName;

            ol.DealContent = this.tbShyj.Value;
            ol.Save();
        }

        ClientScript.RegisterStartupScript(this.GetType(), "RefreshParent", "<script>location=location;</script>");

    }

}
